#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include "FileIO.h"
#include "stdlib.h"
#include <sys/types.h>
#include <dirent.h>

using namespace std;

//Read in data from files

FILE* FileIO::rFile = NULL;
FILE* FileIO::wFile = NULL;

///Default FileIO Constructor

/**Just creates the FileIO type without opening a file*/
FileIO::FileIO() {

    //string writeFilename;
    //wFile = fopen(writeFilename.c_str(), "w");

    //Exception
    /*if (wFile == NULL) {
        printf("Error creating file to write to\n");
    }*/
}

///File opening Constructor

/**This constructor opens a file to read from*/
FileIO::FileIO(string readFilename) {
    rFile = fopen(readFilename.c_str(), "r");

    if (rFile == NULL) {
        printf("Error Opening file. Program terminating\n");
    }
}

///Change file being read from

/**Method for changing what file is currently being open and
 * read from*/
void FileIO::ChangeFile(string Filename) {
    //For changing files to read from
    fclose(rFile);
    rFile = fopen(Filename.c_str(), "r");
}

///Create output

/**Method to open/create a file to write to*/
void FileIO::CreateOutputFile(string output){
	wFile = fopen(output.c_str(), "w");
	if(wFile == NULL){
		printf("Error creating file to write to...Exiting\n");
		exit(1);
	}
}

///Read folder

/**This method reads all the files from a folder and returns a
 vector of strings containg the file path*/
vector<string> FileIO::readFolder(string location) {
    vector <string> list(100);
    int size=0;
    DIR *dir;
    struct dirent *reader;
    dir = opendir(location.c_str());
    if (dir == NULL) {
        puts("Error opening File location");
        exit(1);
    } else {
        reader = readdir(dir);
        do {
            if(reader->d_name[0] != '.'){
                list[size]=location+'/'+reader->d_name;
                size++;
            }
            
            
            reader = readdir(dir);
        } while (reader != NULL);

    }
    list.resize(size);
    closedir(dir);
    return list;
}

///Read from current file

/**Read reads all the characters in a file and returns them
 as a vector char*/
vector<char> FileIO::read() {
    //Variables
    int size=0;
    int i = 0;


    //Gets number of letters in txt file and sets size equal to it
    fseek(rFile, 0, SEEK_END);
    size = ftell(rFile);
    vector<char> word(size);

    //Moves back to beginging of file
    rewind(rFile);
    
    //Reads from file one char at a time
    char c = ' ';

    while (fscanf(rFile, "%c", &c) == 1) {
        if(c == 'A' || c == 'G' || c == 'T' || c == 'C'){
        	word[i] = c;
        	i++;
        }
    }
    word.resize(i);
    return word;
}

///Write char to File

/**is used to write and char passed to it to the current open
 write file*/
void FileIO::Write(char input) {
    fprintf(wFile, "%c", input);

}

///Write int to File

/**is used to write and int passed to it to the current open
 write file*/
void FileIO::Write(int input) {

    fprintf(wFile, "%d", input);
}

///Write string to File

/**is used to write and string passed to it to the current open
 write file*/
void FileIO::Write(string input) {
    fputs(input.c_str(), wFile);
    fprintf(wFile, "\n");

}


//End File reading




